package com.wooboo.server.ext.action.phone.traffic;

import com.wooboo.server.ext.ActionBase;
import com.wooboo.server.ext.ApiError;
import com.wooboo.server.ext.Result;
import com.wooboo.server.ext.data.Ad;
import com.wooboo.server.ext.data.PhoneTrafficGift;
import com.wooboo.server.ext.data.Traffic;
import com.wooboo.server.ext.data.TrafficGift;
import com.wooboo.server.ext.data.UserCompany;
import com.wooboo.server.ext.data.VerificationCode;
import com.wooboo.server.ext.service.DbService;
import com.wooboo.server.ext.util.NumberUtil;
import com.wooboo.server.ext.util.Parameters;
import com.wooboo.server.ext.util.StringUtil;

import net.sf.json.JSONObject;


/**
 * @author www.wooboo.com.cn
 * 流量汇企业信息注册申请
 */
public class Gift extends ActionBase{
	
	public Integer id;
	public String phone_number;
	public String code;	
	
	private DbService dbService = new DbService();
	public Gift(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {
		id = NumberUtil.getInteger(params.getFieldValue("id"));
		code = params.getFieldValue("vcode");
		phone_number = params.getFieldValue("phone_number");
		if (StringUtil.isEmpty(phone_number, code,id)) {
			throw ApiError.Type.INVALID_PARAM.toException();
		}

	}
	
	@Override
	public Result invokeService() throws ApiError {
		Result result = new Result();
		JSONObject data = new JSONObject();
		
		Ad ad = dbService.findAdInfoById(id);
		if(null==ad){
			throw ApiError.Type.EXIST_PARAM.toException("广告信息不存在");
		}
		
		PhoneTrafficGift gift = dbService.findPhoneTrafficGiftByPhoneNumberAndId(ad.plan_id, phone_number);
		if(null!=gift){
			throw ApiError.Type.EXIST_PARAM.toException("您不能重复领取！");
		}
		
		UserCompany com = dbService.findCompanyById(ad.owner);
		if(null==com){
			throw ApiError.Type.EXIST_PARAM.toException("广告主信息不存在");
		}
		
		TrafficGift flow = dbService.findTrafficGift(ad.bid_flow.toString(),ad.plan_id);
		
		//Flow flow = Flow.fromValue(StringUtil.dealNull(ad.bid_flow));
		if(null==flow){
			throw ApiError.Type.EXIST_PARAM.toException("流量包设置不存在。");
		}
		
		PhoneTrafficGift g = new PhoneTrafficGift(flow.getValue(), ad.plan_id, flow.getCode(), "normal", phone_number,null,flow.price,ad.advertiser_id,ad.group_id,ad.id);
		JSONObject response = Traffic.companyFlowPkgHandsel(com, flow,ad,g);
		if(null==response){
			throw ApiError.Type.INVALID_PARAM.toException("调用流量汇接口 【companyFlowPkgHandsel】失败！");
		}
		String status = response.optString("status");
		String code=response.optString("code");
		if("ok".equalsIgnoreCase(status) && "0".equals(code)){
            data.put("message", "流量领取申请已提交,请关注流量余额情况!");
            dbService.savephoneTrafficGift(g);
		}else{
			//throw ApiError.Type.INVALID_PARAM.toException("调用流量汇注册接口失败！status="+status+",code="+code+"】");
			throw ApiError.Type.INVALID_PARAM.toException("调用流量汇接口【companyFlowPkgHandsel】失败！status="+status+",code="+code+",message="+response.optString("message")+"");
		}
		
		result.put("data", data);
	    return result;
	}
	
	
	
}
